package com.daogu.fuelcard.service.impl;

import com.daogu.fuelcard.dao.OilCardMapper;
import com.daogu.fuelcard.dao.RequestRecordMapper;
import com.daogu.fuelcard.dao.TransactionRecordMapper;
import com.daogu.fuelcard.entity.OilCard;
import com.daogu.fuelcard.entity.RequestRecord;
import com.daogu.fuelcard.entity.TransactionRecord;
import com.daogu.fuelcard.enums.CheckStatusEnum;
import com.daogu.fuelcard.service.OilCardService;
import com.daogu.fuelcard.service.RequestRecordService;
import com.daogu.fuelcard.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RequestRecordServiceImpl implements RequestRecordService {

    @Autowired
    private RequestRecordMapper requestRecordMapper;

    @Autowired
    private OilCardMapper oilCardMapper;

    @Autowired
    private TransactionRecordMapper transactionRecordMapper;

    @Override
    public Result<Void> insert(RequestRecord requestRecord) {
        requestRecordMapper.insert(requestRecord);
        OilCard oilCard = oilCardMapper.selectByCardNo(requestRecord.getCardNo());
        if (oilCard == null){
            throw new RuntimeException("该卡号不存在");
        }
        BigDecimal balance = oilCard.getBalance().add(requestRecord.getRequestAmount());
        oilCardMapper.updateBalance(requestRecord.getCardNo(),balance);
        return Result.accept();
    }

    @Override
    public Result<Void> deletedById(Integer id) {
        requestRecordMapper.deletedById(id);
        RequestRecord requestRecord = requestRecordMapper.selectById(id);
        OilCard oilCard = oilCardMapper.selectByCardNo(requestRecord.getCardNo());
        if (oilCard == null){
            throw new RuntimeException("该卡号不存在");
        }
        BigDecimal balance = oilCard.getBalance().subtract(requestRecord.getRequestAmount());
        oilCardMapper.updateBalance(requestRecord.getCardNo(),balance);
        return Result.accept();
    }

    @Override
    public Result<Void> updateWriteOffAmount(Integer id) {

        RequestRecord requestRecord = requestRecordMapper.selectById(id);
        String cardNo = requestRecord.getCardNo();
        OilCard oilCard = oilCardMapper.selectByCardNo(cardNo);

        List<TransactionRecord> unCheckTransaction = transactionRecordMapper.getUnCheckTransaction(cardNo);
        if (!unCheckTransaction.isEmpty()) {
            BigDecimal writeOffAmount = unCheckTransaction.stream()
                    .map(TransactionRecord::getTotalPrice)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);

            if (writeOffAmount.compareTo(requestRecord.getRequestAmount())>0) {
                throw new RuntimeException("核销金额大于请款金额");
            }

            if (oilCard.getBalance().compareTo(writeOffAmount)>0) {
                throw new RuntimeException("油卡余额不足");
            }
            oilCardMapper.updateBalance(cardNo,oilCard.getBalance().subtract(writeOffAmount));
            requestRecordMapper.updateWriteOffAmount(id,writeOffAmount);
            List<Integer> transactionIds = unCheckTransaction.stream().map(TransactionRecord::getId).toList();
            transactionRecordMapper.updateCheckStatus(transactionIds, CheckStatusEnum.CHECK_FAIL.getCode());
        }
        return Result.accept();
    }

    @Override
    public Result<PageInfo<RequestRecord>> getRequestRecord(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<RequestRecord> requestRecords = requestRecordMapper.selectAll();
        PageInfo<RequestRecord> pageInfo = new PageInfo<>(requestRecords);
        return Result.accept(pageInfo);
    }
}
